package com.jianan.mis.app.overseastudy;

import static com.jianan.mis.util.ui.calendar.SWTCalendarWrapper.openCalendar;
import static com.jianan.mis.util.ui.table.TableCreator.createTableColumn;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.jianan.mis.data.domain.OsContract;
import com.jianan.mis.data.domain.OsContractMaterial;
import com.jianan.mis.data.domain.OsContractPlan;
import com.jianan.mis.data.domain.OsContractSurvey;
import com.jianan.mis.service.OsContractService;
import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.ImageManager;
import com.jianan.mis.sys.ResourceManager;
import com.jianan.mis.sys.SpringBeanFactory;
import com.jianan.mis.util.StringUtils;
import com.jianan.mis.util.ui.calendar.SWTCalendarDialog;
import com.jianan.mis.util.ui.calendar.SWTCalendarEvent;
import com.jianan.mis.util.ui.calendar.SWTCalendarListener;

public class OsMaterialDialog extends Dialog {
	
	private StyledText txtSurveyResult;
	private StyledText txtRemark;
	private Text txtCOEDate;
	private Text txtRemitTuitionDate;
	private StyledText txtNoticeRemark;
	private Text txtNoticeBodyExamDate;
	private Text txtPVADate;
	private Combo comboMaterialState;
	private Text txtOfferArriveDate;
	private Text txtApplyOfferDate;
	
	private int mode = -1;
	
	private boolean readonly;
	
	private OsContract osContract;
	
	private OsContractPlan osContractPlan;
	
	private List<OsContractMaterial> osContractMaterials;
	
	private List<OsContractSurvey> osContractSurveys;
	
	private OsContractService osContractService;
	
	private boolean materialModified = false;
	
	private boolean surveyModified = false;
	
	private String[] materialStates = new String[]{"已移交材料部", "等待语言成绩"};

	class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object element, int columnIndex) {
			OsContractMaterial osContractMaterial = (OsContractMaterial) element;
			String text = materials.getColumn(columnIndex).getText();
			if (text.equals(MATERIAL_NAME)) {
				return osContractMaterial.getMaterial();
			} else if (text.equals(COLLECTED)) {
				if(osContractMaterial.isCollected())
					return "是";
				else
					return "否";
			} else if (text.equals(TRANSLATED)) {
				if(osContractMaterial.isTranslated())
					return "是";
				else
					return "否";
			} else {
				return null;
			}
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	class CellModifier implements ICellModifier {
		public boolean canModify(Object element, String property) {
			//OsContractMaterial osContractMaterial = (OsContractMaterial) element;
			if (property.equals(MATERIAL_NAME)) {
				return true;
			} else if (property.equals(COLLECTED)) {
					//&& osContractMaterial.isCollected() == false) {
				return true;
			} else if (property.equals(TRANSLATED)) {
					//&& osContractMaterial.isCollected() == true
					//&& osContractMaterial.isTranslated() == false) {
				return true;
			} else {
				return false;
			}
		}
		public Object getValue(Object element, String property) {
			OsContractMaterial osContractMaterial = (OsContractMaterial) element;
			if (property.equals(MATERIAL_NAME)) {
				return osContractMaterial.getMaterial();
			} else if (property.equals(COLLECTED)) {
				return osContractMaterial.isCollected();
			} else if (property.equals(TRANSLATED)) {
				return osContractMaterial.isTranslated();
			} else {
				return null;
			}
		}
		public void modify(Object element, String property, Object value) {
			TableItem item = (TableItem) element;
			OsContractMaterial osContractMaterial = (OsContractMaterial) item.getData();
			if (property.equals(MATERIAL_NAME)) {
				String newValue = (String) value;
				if (!newValue.equals(osContractMaterial.getMaterial())
						&& MessageDialog.openConfirm(getShell(),
								IMessageKeys.CONFIRM, "确认修改 "
										+ osContractMaterial.getMaterial()
										+ " 为 " + newValue + " 吗?")) {
					materialModified = true;
					osContractMaterial.setMaterial(newValue);
				}
			} else if (property.equals(COLLECTED)) {
				Boolean collected = (Boolean) value;
				if (MessageDialog.openConfirm(getShell(), IMessageKeys.CONFIRM,
						"确认修改 " + osContractMaterial.getMaterial() + " 为" + 
						(collected ? "已" : "未") + "收集完毕吗?")) {
					materialModified = true;
					osContractMaterial.setCollected(collected);
				}
			} else if (property.equals(TRANSLATED)) {
				Boolean translated = (Boolean) value;
				if (MessageDialog.openConfirm(getShell(), IMessageKeys.CONFIRM,
						"确认 " + osContractMaterial.getMaterial() + " 为" +
						(translated ? "已" : "未") + "翻译完毕吗?")) {
					materialModified = true;
					osContractMaterial.setTranslated(translated);
				}
			}
			materialTableViewer.refresh();
		}
	}
	class MaterialSorter extends ViewerSorter {
		public int compare(Viewer viewer, Object e1, Object e2) {
			OsContractMaterial osContractMaterial1 = (OsContractMaterial) e1;
			OsContractMaterial osContractMaterial2 = (OsContractMaterial) e2;
			return osContractMaterial1.getMaterial().compareTo(osContractMaterial2.getMaterial());
		}
	}
	class ListContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	class ListLabelProvider extends LabelProvider {
		public String getText(Object element) {
			OsContractSurvey osContractSurvey = (OsContractSurvey) element;
			return dateFormat.format(osContractSurvey.getSurveyDate());
		}
		public Image getImage(Object element) {
			return null;
		}
	}
	private TableViewer materialTableViewer;
	private Table materials;
	private ListViewer surveyListViewer;
	private org.eclipse.swt.widgets.List surveys;
	final private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public OsMaterialDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		final GridLayout gridLayout_parent = new GridLayout();
		gridLayout_parent.numColumns = 3;
		composite.setLayout(gridLayout_parent);
	
		final Label left_label = new Label(composite, SWT.NONE);
		final GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		left_label.setLayoutData(gd_label);

		final ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL | SWT.H_SCROLL);
		scrolledComposite.setAlwaysShowScrollBars(false);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height - 150 > 585 ? 585 : screenSize.height - 150;
		scrolledComposite.setLayoutData(new GridData(555, height));
		
		//Composite container = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(scrolledComposite, SWT.NONE);
		final GridData gd_container = new GridData();
		container.setLayoutData(gd_container);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.numColumns = 4;
		gridLayout.marginTop = 5;
		gridLayout.marginRight = 5;
		gridLayout.marginLeft = 5;
		container.setLayout(gridLayout);

		final CLabel label_1 = new CLabel(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1));
		label_1.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label_1.setText("递件前:");
		label_1.setImage(ImageManager.getImage(IImageKeys.RESOURCES));

		final Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		label.setText("材料清单:");

		materialTableViewer = new TableViewer(container, SWT.FULL_SELECTION | SWT.BORDER);
		materialTableViewer.setSorter(new MaterialSorter());
		materialTableViewer.setLabelProvider(new TableLabelProvider());
		materialTableViewer.setContentProvider(new ContentProvider());
		materials = materialTableViewer.getTable();
		materials.setLinesVisible(true);
		materials.setHeaderVisible(true);
		final GridData gd_materials = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
		gd_materials.heightHint = 140;
		materials.setLayoutData(gd_materials);
		
		new Label(container, SWT.NONE);

		final Button btnAddMaterial = new Button(container, SWT.NONE);
		btnAddMaterial.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				InputDialog inputDialog = new InputDialog(getShell(), "添加材料",
						"请输入材料名称:", "", new IInputValidator() {
							public String isValid(String newText) {
								if (StringUtils.isEmpty(newText)) {
									return "材料名称不得为空!";
								}
								for (OsContractMaterial material : osContractMaterials) {
									if (material.getMaterial().equals(newText)) {
										return "材料名称不得重复!";
									}
								}
								return null;
							}
						});
				if(inputDialog.open() == Window.OK) {
					materialModified = true;
					OsContractMaterial material = new OsContractMaterial();
					material.setMaterial(inputDialog.getValue());
					material.setOsContract(osContract);
					material.setCollected(false);
					material.setTranslated(false);
					osContractMaterials.add(material);
					materialTableViewer.refresh();
				}
			}
		});
		final GridData gd_btnAddMaterial = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		btnAddMaterial.setLayoutData(gd_btnAddMaterial);
		btnAddMaterial.setText("添加材料");
		btnAddMaterial.setImage(ImageManager.getImage(IImageKeys.PLUS));
		
		new Label(container, SWT.NONE);

		final Button btnDelMaterial = new Button(container, SWT.NONE);
		btnDelMaterial.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) materialTableViewer
						.getSelection();
				OsContractMaterial material = (OsContractMaterial) selection
						.getFirstElement();
				if (material == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一件材料!");
				} else {
					if (MessageDialog.openConfirm(e.display.getActiveShell(), 
							IMessageKeys.CONFIRM, "确认删除 " + material.getMaterial() + " 吗?")) {
						materialModified = true;
						osContractMaterials.remove(materials.getSelectionIndex());
						materialTableViewer.refresh();
					}
				}
			}
		});
		final GridData gd_btnDelMaterial = new GridData();
		btnDelMaterial.setLayoutData(gd_btnDelMaterial);
		btnDelMaterial.setText("删除材料");
		btnDelMaterial.setImage(ImageManager.getImage(IImageKeys.MINUS));
		
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_2.setText("申请Offer日期:");

		txtApplyOfferDate = new Text(container, SWT.BORDER);
		txtApplyOfferDate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtApplyOfferDate, dateFormat);
			}
		});
		final GridData gd_txtApplyOfferDate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtApplyOfferDate.widthHint = 90;
		txtApplyOfferDate.setLayoutData(gd_txtApplyOfferDate);

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_3.setText("Offer到达日期:");

		txtOfferArriveDate = new Text(container, SWT.BORDER);
		txtOfferArriveDate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtOfferArriveDate, dateFormat);
			}
		});
		final GridData gd_txtOfferArriveDate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtOfferArriveDate.widthHint = 90;
		txtOfferArriveDate.setLayoutData(gd_txtOfferArriveDate);
		
		final Label label_16 = new Label(container, SWT.NONE);
		label_16.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_16.setText("状态:");

		comboMaterialState = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboMaterialState = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		comboMaterialState.setLayoutData(gd_comboMaterialState);
		comboMaterialState.setItems(materialStates);
		
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final CLabel label_4 = new CLabel(container, SWT.NONE);
		label_4.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label_4.setText("递件后:");
		label_4.setImage(ImageManager.getImage(IImageKeys.RESOURCES));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label_6 = new Label(container, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_6.setText("PVA或面签日期:");

		txtPVADate = new Text(container, SWT.BORDER);
		txtPVADate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtPVADate, dateFormat);
			}
		});
		final GridData gd_txtPVADate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtPVADate.widthHint = 90;
		txtPVADate.setLayoutData(gd_txtPVADate);

		final Label label_7 = new Label(container, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_7.setText("通知学生体检汇款日期:");

		txtNoticeBodyExamDate = new Text(container, SWT.BORDER);
		txtNoticeBodyExamDate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtNoticeBodyExamDate, dateFormat);
			}
		});
		final GridData gd_txtNoticeBodyExamDate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtNoticeBodyExamDate.widthHint = 90;
		txtNoticeBodyExamDate.setLayoutData(gd_txtNoticeBodyExamDate);

		final Label label_8 = new Label(container, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		label_8.setText("通知备注:");

		txtNoticeRemark = new StyledText(container, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
		txtNoticeRemark.setWordWrap(true);
		final GridData gd_txtNoticeRemark = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtNoticeRemark.heightHint = 30;
		gd_txtNoticeRemark.widthHint = 335;
		txtNoticeRemark.setLayoutData(gd_txtNoticeRemark);

		final Label label_9 = new Label(container, SWT.NONE);
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_9.setText("学费汇出日期:");

		txtRemitTuitionDate = new Text(container, SWT.BORDER);
		txtRemitTuitionDate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtRemitTuitionDate, dateFormat);
			}
		});
		final GridData gd_txtRemitTuitionDate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtRemitTuitionDate.widthHint = 90;
		txtRemitTuitionDate.setLayoutData(gd_txtRemitTuitionDate);

		final Label label_10 = new Label(container, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_10.setText("COE日期:");

		txtCOEDate = new Text(container, SWT.BORDER);
		txtCOEDate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtCOEDate, dateFormat);
			}
		});
		final GridData gd_txtCOEDate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtCOEDate.widthHint = 90;
		txtCOEDate.setLayoutData(gd_txtCOEDate);
		
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		final CLabel label_11 = new CLabel(container, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 4, 1));
		label_11.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label_11.setText("模拟调查:");
		label_11.setImage(ImageManager.getImage(IImageKeys.RESOURCES));

		new Label(container, SWT.NONE);

		final Label label_14 = new Label(container, SWT.NONE);
		label_14.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_14.setText("调查日期");

		final Label label_15 = new Label(container, SWT.NONE);
		label_15.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_15.setText("调查结果");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		surveyListViewer = new ListViewer(container, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		surveyListViewer.setContentProvider(new ListContentProvider());
		surveyListViewer.setLabelProvider(new ListLabelProvider());
		surveys = surveyListViewer.getList();
		surveys.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(surveys.getSelectionIndex() >= 0
						&&surveys.getSelectionIndex() < osContractSurveys.size()) {
					OsContractSurvey survey = osContractSurveys.get(surveys.getSelectionIndex());
					txtSurveyResult.setText(survey.getSurveyResult());
				}
			}
		});
		final GridData gd_list = new GridData(SWT.RIGHT, SWT.BOTTOM, false, false);
		gd_list.widthHint = 90;
		gd_list.heightHint = 62;
		surveys.setLayoutData(gd_list);

		txtSurveyResult = new StyledText(container, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
		txtSurveyResult.setWordWrap(true);
		final GridData gd_txtSurveyResult = new GridData(SWT.LEFT, SWT.BOTTOM, true, false, 2, 1);
		gd_txtSurveyResult.heightHint = 60;
		gd_txtSurveyResult.widthHint = 200;
		txtSurveyResult.setLayoutData(gd_txtSurveyResult);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Button btnAddSurvey = new Button(container, SWT.NONE);
		btnAddSurvey.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				final SWTCalendarDialog cal = new SWTCalendarDialog(e.display);
				cal.addDateChangedListener(new SWTCalendarListener() {
					public void dateChanged(SWTCalendarEvent calendarEvent) {
						if (MessageDialog.openConfirm(e.display.getActiveShell(), 
								IMessageKeys.CONFIRM, "确认创建 " + 
								dateFormat.format(calendarEvent.getCalendar().getTime()) +
								" 的调查吗?")) {
							surveyModified = true;
							OsContractSurvey survey = new OsContractSurvey();
							survey.setOsContract(osContract);
							survey.setSurveyDate(calendarEvent.getCalendar().getTime());
							osContractSurveys.add(survey);
							surveyListViewer.refresh();
							surveys.select(osContractSurveys.size() - 1);
							txtSurveyResult.setText("");
							txtSurveyResult.setFocus();
							MessageDialog.openInformation(e.display.getActiveShell(),
									IMessageKeys.SUCCESS, "添加调查成功，请输入调查结果!");
						}
					}
				});
				cal.open();
			}
		});
		btnAddSurvey.setText("添加调查");

		final Button btnUpdateSurvey = new Button(container, SWT.NONE);
		btnUpdateSurvey.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (surveys.getSelectionIndex() < 0) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一次调查!");
					return;
				}
				surveyModified = true;
				OsContractSurvey survey = osContractSurveys.get(surveys.getSelectionIndex());
				survey.setSurveyResult(txtSurveyResult.getText());
				MessageDialog.openInformation(e.display.getActiveShell(),
						IMessageKeys.SUCCESS, "调查结果保存成功!");
			}
		});
		btnUpdateSurvey.setText("保存调查结果");

		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final CLabel label_5 = new CLabel(container, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
		label_5.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label_5.setText("备注:");
		label_5.setImage(ImageManager.getImage(IImageKeys.RESOURCES));

		txtRemark = new StyledText(container, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
		txtRemark.setWordWrap(true);
		final GridData gd_txtRemark = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtRemark.heightHint = 51;
		gd_txtRemark.widthHint = 335;
		txtRemark.setLayoutData(gd_txtRemark);

		initializeTableViewer();
		initializeServices();
		fillPlan();
		
		if (readonly) {
			container.setEnabled(false);
		}
		
		if (mode == 0) {
			// 递件前
			txtPVADate.setEnabled(false);
			txtNoticeBodyExamDate.setEnabled(false);
			txtNoticeRemark.setEnabled(false);
			txtRemitTuitionDate.setEnabled(false);
			txtCOEDate.setEnabled(false);
		} else {
			// 递件后
			txtApplyOfferDate.setEnabled(false);
			txtOfferArriveDate.setEnabled(false);
			comboMaterialState.setEnabled(false);
		}

		container.setSize(container.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrolledComposite.setContent(container);
		
		final Label right_label = new Label(composite, SWT.NONE);
		final GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		right_label.setLayoutData(gd_label_1);
		
		//
		return composite;
	}
	
	public static final String MATERIAL_NAME = "材料名";
	public static final String COLLECTED = "是否收集完毕";
	public static final String TRANSLATED = "是否翻译完毕";
	
	private void initializeTableViewer() {
		createTableColumn(materialTableViewer, SWT.CENTER, MATERIAL_NAME, "", 150, null);
		createTableColumn(materialTableViewer, SWT.CENTER, COLLECTED, "", 100, null);
		createTableColumn(materialTableViewer, SWT.CENTER, TRANSLATED, "", 100, null);
		
		materialTableViewer.setColumnProperties(new String[] { MATERIAL_NAME, COLLECTED, TRANSLATED });
		materialTableViewer.setCellModifier(new CellModifier());
        CellEditor[] editors = new CellEditor[materials.getColumnCount()];
        editors[0] = new TextCellEditor(materialTableViewer.getTable());
        editors[1] = new CheckboxCellEditor(materialTableViewer.getTable());
        editors[2] = new CheckboxCellEditor(materialTableViewer.getTable());
        materialTableViewer.setCellEditors(editors);
	}
	
	private void initializeServices() {
		osContractService = (OsContractService) SpringBeanFactory.getBean("osContractService");
	}
	
	public void setMode(int mode) {
		this.mode = mode;
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public void setOsContract(OsContract osContract) {
		this.osContract = osContract;
	}
	
	private void fillPlan() {
		// 填充进度表
		osContractPlan = osContractService.getPlan(osContract);
		if (osContractPlan.getApplyOfferDate() != null) {
			txtApplyOfferDate.setText(dateFormat.format(osContractPlan
					.getApplyOfferDate()));
		}
		if (osContractPlan.getOfferArriveDate() != null) {
			txtOfferArriveDate.setText(dateFormat.format(osContractPlan
					.getOfferArriveDate()));
		}
		if (osContractPlan.getMaterialState().equals(0)) {
			comboMaterialState.select(0);
		} else {
			comboMaterialState.select(1);
		}
		if (osContractPlan.getPvaDate() != null) {
			txtPVADate.setText(dateFormat.format(osContractPlan.getPvaDate()));
		}
		if (osContractPlan.getNoticeBodyExamDate() != null) {
			txtNoticeBodyExamDate.setText(dateFormat.format(osContractPlan
					.getNoticeBodyExamDate()));
		}
		if (StringUtils.isNotEmpty(osContractPlan.getNoticeRemark())) {
			txtNoticeRemark.setText(osContractPlan.getNoticeRemark());
		}
		if (osContractPlan.getRemitTuitionDate() != null) {
			txtRemitTuitionDate.setText(dateFormat.format(osContractPlan
					.getRemitTuitionDate()));
		}
		if (osContractPlan.getCoeDate() != null) {
			txtCOEDate.setText(dateFormat.format(osContractPlan.getCoeDate()));
		}
		if (StringUtils.isNotEmpty(osContractPlan.getRemark())) {
			txtRemark.setText(osContractPlan.getRemark());
		}
		// 填充材料
		osContractMaterials = osContractService.getMaterials(osContract);
		materialTableViewer.setInput(osContractMaterials);
		// 填充调查
		osContractSurveys = osContractService.getSurveys(osContract);
		surveyListViewer.setInput(osContractSurveys);
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		if (readonly) {
			createButton(parent, IDialogConstants.OK_ID,
					IDialogConstants.OK_LABEL, true);
		} else {
			createButton(parent, IDialogConstants.OK_ID, "保存", true);
			createButton(parent, IDialogConstants.CANCEL_ID,
					IDialogConstants.CANCEL_LABEL, false);
		}
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(600, 695);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("留学签约作件进度表");
	}
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			if (!readonly) {
				try {
					osContractPlan.setRemark(txtRemark.getText());
					if (mode == 0) {
						// 递件前
						try {
							osContractPlan.setApplyOfferDate(dateFormat.parse(txtApplyOfferDate.getText()));
						} catch (ParseException e) {
							e.printStackTrace();
							osContractPlan.setApplyOfferDate(null);
						}
						try {
							osContractPlan.setOfferArriveDate(dateFormat.parse(txtOfferArriveDate.getText()));
						} catch (ParseException e) {
							e.printStackTrace();
							osContractPlan.setOfferArriveDate(null);
						}
						osContractPlan.setMaterialState(comboMaterialState.getSelectionIndex());
						osContractService.updatePlan(osContractPlan);
						if (materialModified)
							osContractService.updateMaterials(osContract,
									osContractMaterials);
						if (surveyModified)
							osContractService.updateSurveys(osContract,
									osContractSurveys);
					} else {
						// 递件后
						try {
							osContractPlan.setPvaDate(dateFormat.parse(txtPVADate.getText()));
						} catch (ParseException e) {
							e.printStackTrace();
							osContractPlan.setPvaDate(null);
						}
						try {
							osContractPlan.setNoticeBodyExamDate(dateFormat.parse(txtNoticeBodyExamDate.getText()));
						} catch (ParseException e) {
							e.printStackTrace();
							osContractPlan.setNoticeBodyExamDate(null);
						}
						osContractPlan.setNoticeRemark(txtNoticeRemark.getText());
						try {
							osContractPlan.setRemitTuitionDate(dateFormat.parse(txtRemitTuitionDate.getText()));
						} catch (ParseException e) {
							e.printStackTrace();
							osContractPlan.setRemitTuitionDate(null);
						}
						try {
							osContractPlan.setCoeDate(dateFormat.parse(txtCOEDate.getText()));
						} catch (ParseException e) {
							e.printStackTrace();
							osContractPlan.setCoeDate(null);
						}
						osContractService.updatePlan(osContractPlan);
						if (surveyModified)
							osContractService.updateSurveys(osContract,
									osContractSurveys);
					}
					MessageDialog.openInformation(getShell(),
							IMessageKeys.SUCCESS, "作件进度单保存成功!");
				} catch (Exception ex) {
					MessageDialog.openError(getShell(), IMessageKeys.FAIL, ex
							.getMessage());
				}		
			}
		}
		super.buttonPressed(buttonId);
	}

}
